<?php
  /**
   * iJMenu
   *
   * The iJMenu class :description
   *
   * @copyright Copyright (c) 2011, iJBLOKS Progect
   */
    class AdminMenuControl extends iJBlock {
        
        /**
         *
         * Class Constructuctor
         * @access public
         */
        public function __construct() {
    
        }
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          $iJAdminMenu =  iJFactory::singleton('AdminMenu');
          $iJLanguages =  iJFactory::singleton('iJLanguages');
          $iJMenu      =  iJFactory::singleton('iJMenu');
          
          if(isset($_POST['create'])) {
            $iJMenu->createMenuItem($_POST);
          }
          
          if(isset($_POST['edit'])) {
            $iJMenu->editMenuItem($_POST);
          }
          
          if(isset($_GET['delID'])) {
            $iJMenu->removeMenuItem($_GET['delID']);
          }
          
          $page   = isset($_REQUEST['page'])                                     ? $_REQUEST['page']    : 1;
          $parent = isset($_REQUEST['parent'])                                   ? $_REQUEST['parent']  : 0;
          $search = isset($_REQUEST['search'])    && !empty($_REQUEST['search']) ? $_REQUEST['search']  : '';
          $menuList = $iJAdminMenu->getMenuList($page, $search, $parent);
          
          $menu     = $menuList[0];
          $Qmenu    = $menuList[1];
          
          $urlsList = iJHrefLink::getAllUrls();
          asort($urlsList);
          $pages_keys = array();
          foreach($urlsList as $page_key => $url) {
            $pages_keys[] = $page_key;
          }
          
          $this->addToken('menu',      $menu);         
          $this->addToken('search',      $search);
          $this->addToken('page',        $page);
          $this->addToken('parent',     $parent);
          $this->addToken('pages_keys',     $pages_keys);
          
          $this->addToken('set_icon',   iJHrefLink::Icon('settings', '16x16'));
          $this->addToken('item_icon',   iJHrefLink::Icon('folder', '16x16'));
          $this->addToken('delete_icon', iJHrefLink::Icon('trash', '16x16'));
          $this->addToken('edit_icon',   iJHrefLink::Icon('edit', '16x16'));
          
          $this->addToken('form_action', iJHrefLink::linkToLocalPage('admin_menu') . '?' . iJGetAllGetParams()); 
          $this->addToken('back_link',   $iJAdminMenu->getBackLink($parent));
          $this->addToken('curLanguage', $iJLanguages->getLanguage());
          $this->addToken('languages',   $iJLanguages->getLanguagesExeptCurrent());  
          
          $this->addToken('total_items',    $Qmenu->getSize());
          $this->addToken('pages_count',    $Qmenu->getPagesCount());
          $this->addToken('prev_link',      $Qmenu->getPerviousLink($page, 'admin_menu', $_GET));
          $this->addToken('next_link',      $Qmenu->getNextLink($page,     'admin_menu', $_GET));
          
          return parent::execute();
        }
        
    }
?>